//
//  LEEditController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEEditController.h"
#import "LEStoreModel.h"
#import "MakerTools.h"

@interface LEEditController ()
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *goodsField;
/**
 商店模型
 */
@property(nonatomic,strong)LEStoreModel *storeModel;

@end

@implementation LEEditController

- (void)setupUI
{
    [super setupUI];
    self.view.backgroundColor = LEBackgroundColor;
    self.title = @"修改资料";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveClick:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)loadData
{
    //从沙盒中读取用户信息
    LEStoreModel *storeModel = [MakerTools readFromSandBox:Store_Info];
    self.storeModel = storeModel;
    self.addressField.text = storeModel.store_address;
    self.phoneField.text = storeModel.store_tel;
    self.goodsField.text = storeModel.store_zy;
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *paramString = [NSString stringWithFormat:@"store_address|%@,store_tel|%@,store_zy|%@",self.addressField.text,self.phoneField.text,self.goodsField.text];
    [dict setObject:paramString forKey:@"param"];
    
    [SVProgressHUD showWithStatus:@"正在保存"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:OwnSaveStoreInfoUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            //更新本地商店信息
            [weakSelf updateLocalStoreInfo];
        }

    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark- 点击事件
- (void)saveClick:(UIButton *)sender
{
    LELog(@"%s---%d---保存",__func__,__LINE__);
    if ([self.phoneField.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"手机号不能为空"];
        return;
    }
    if ([self.addressField.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"店铺地址不能为空"];
        return;
    }
    if ([self.goodsField.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"主营商品不能为空"];
        return;
    }
    [self requestRefreshData];
}

#pragma mark- privateMethod

/**
 更新商店信息
 */
- (void)updateLocalStoreInfo
{
    self.storeModel.store_address = self.addressField.text;
    self.storeModel.store_tel = self.phoneField.text;
    self.storeModel.store_zy = self.goodsField.text;
    
    [MakerTools saveToSandBox:Store_Info withSavedObject:self.storeModel];
}
@end










